package br.com.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import br.com.connection.Connection;
import br.com.model.Item;
import br.com.model.Lance;
import br.com.model.Usuario;

public class ItemDao {

	public static boolean inserirItem(Item item) {
		EntityManager manager = Connection.criarConexao();
		try {

			manager.getTransaction().begin();
			item.setIsAtivo(1);
			manager.persist(item);
			manager.getTransaction().commit();
			manager.close();

		} catch (Exception e) {
			System.out.println("Erro" + e);
			return false;
		} finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return true;
	}

	@SuppressWarnings("finally")
	public static boolean excluirItem(Item item) {
		EntityManager manager = Connection.criarConexao();

		try {

			manager.getTransaction().begin();
			item.setIsAtivo(0);
			manager.merge(item);
			manager.getTransaction().commit();
			manager.close();
		} catch (Exception ex) {
			System.out.println("Erro: " + ex);
			return false;
		} finally {
			if (manager.isOpen()) {
				manager.close();
			}
			return true;
		}
	}

	public static boolean alterarItem(Item item) {
		EntityManager manager = Connection.criarConexao();

		try {

			manager.getTransaction().begin();
			manager.merge(item);
			manager.getTransaction().commit();
			manager.close();
		} catch (Exception e) {
			System.out.println("Erro" + e);
			return false;
		} finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return true;
	}

	public static ArrayList<Item> listarItem() {
		EntityManager manager = Connection.criarConexao();
		ArrayList<Item> lista = new ArrayList<>();
		try {

			manager.getTransaction().begin();
			CriteriaBuilder cb = manager.getCriteriaBuilder();
			CriteriaQuery<Item> ci = cb.createQuery(Item.class);
			Root<Item> ri = ci.from(Item.class);
			ci.select(ri);
			Predicate predicate = cb.equal(ri.get("isAtivo"), 1);
			ci.where(predicate);
			TypedQuery<Item> query = manager.createQuery(ci);
			lista = (ArrayList<Item>) query.getResultList();
			manager.getTransaction().commit();
			manager.close();

		} catch (Exception e) {
			System.out.println("Erro" + e);
		} finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return lista;

	}

	public static Item pesquPorId(int id) {
		Item item = new Item();
		EntityManager manager = Connection.criarConexao();
		try {
			
			item = manager.find(Item.class, id);
			
			manager.close();

		} catch (Exception e) {
			System.out.println("Erro" + e);
		}finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return item;
	}

	public static ArrayList<Item> listarItemForaLeilao() {
		EntityManager manager = Connection.criarConexao();
		ArrayList<Item> item = new ArrayList<Item>();
		try{
		manager.getTransaction().begin();
		Query query = manager
				.createQuery("from Item where inLeilao = :inLeilao and isAtivo = :isAtivo");
		query.setParameter("inLeilao", 0);
		query.setParameter("isAtivo", 1);
		item = (ArrayList<Item>) query.getResultList();
		manager.getTransaction().commit();

		manager.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			if (manager.isOpen()) {
				manager.close();
			}
		}
		return item;
	}

}
